﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Audio;

namespace mdk.Cenas
{
    class Menu
    {

        KeyboardState teclado, teclado_anterior;    

        Rectangle obj;

        enum Options { NEW_GAME, LOAD_GAME, OPTIONS_MENU, CREDITS, EXIT, Count };
        Options current_option;

        String[] menuLabels = new String[] { "New Game", "Saved Game", "Options", "Credits", "Quit" };

        //Song bgm;
        SoundEffect change_option, select_option;
    
        SpriteFont menuFont, menuFontSelected;

        Video video;
        VideoPlayer player;
        Texture2D last_frame;
        bool replayVideo;          //pra rodar o video só uma vez

        public Menu(ContentManager content)
        {                               

            current_option = Options.NEW_GAME;

            //bgm = content.Load<Song>("audio/mdk");
            select_option = content.Load<SoundEffect>("audio/BEEP1C");
            change_option = content.Load<SoundEffect>("audio/BEEP2C");            

            menuFont = content.Load<SpriteFont>("font/MenuFont");
            menuFontSelected = content.Load<SpriteFont>("font/MenuFontSelected");

            video = content.Load<Video>("MDK12");
            player = new VideoPlayer();
            last_frame = content.Load<Texture2D>("img/Menu/last_frame");
            
            replayVideo = true;      

        }

        public void Update(GameTime gameTime)
        {

            if (replayVideo)
            {
                player.Play(video);
                replayVideo = false;
            }

            HandleInput();

            

        }

        public void Draw(GameTime gameTime, SpriteBatch tela)
        {

            DrawBackgroud(gameTime, tela);

            DrawOptions(tela);            

        }

        private void DrawBackgroud(GameTime gameTime, SpriteBatch tela)
        {
            
            Texture2D videoTexture = null;

            if (player.State != MediaState.Stopped)
                videoTexture = player.GetTexture();
            else
                videoTexture = last_frame;         


            if (videoTexture != null)
            {
                tela.Draw(videoTexture, new Rectangle(0, 45, 700, 420), Color.White);
            }


        }


        private void DrawOptions(SpriteBatch tela)
        {
            
            Vector2 position = new Vector2(20, 45);
            SpriteFont font;
            String label;

            for (int i = 0; i < menuLabels.Length; i++)
            {

                font = (i == (int)current_option ? menuFontSelected : menuFont);
                label = menuLabels[i];

                
                if ((Options) i == Options.LOAD_GAME)
                    tela.DrawString(font, label, position, Color.Silver);
                else
                    tela.DrawString(font, label, position, Color.Goldenrod);


                position.Y += 40;

            }            

        }

        private void HandleInput()
        {

            teclado = Keyboard.GetState();

            //alterna opções
            if (teclado.IsKeyDown(Keys.Up) && (teclado_anterior != teclado))
            {
                current_option--;
                change_option.Play();
            }
            else if (teclado.IsKeyDown(Keys.Down) && (teclado_anterior != teclado))
            {
                current_option++;
                change_option.Play();
            }


            //realiza o loop entre as opções
            if (current_option == Options.Count)
                current_option = 0;
            if (current_option < 0)
                current_option = Options.Count - 1;


            if (teclado.IsKeyDown(Keys.Enter) && (teclado_anterior != teclado))
            {
                select_option.Play();

                switch (current_option)
                {
                    case Options.NEW_GAME:
                        Gerenciador.cena_atual = Gerenciador.Cena.FASE_1;
                        break;
                    case Options.CREDITS:
                        Gerenciador.cena_atual = Gerenciador.Cena.CREDITS;
                        break;
                    case Options.OPTIONS_MENU:
                        Gerenciador.cena_atual = Gerenciador.Cena.OPTIONS;                        
                        break;
                    case Options.EXIT:
                        Gerenciador.cena_atual = Gerenciador.Cena.EXIT_GAME;
                        break;

                }


            }

            teclado_anterior = teclado;

        }       

    }
}
